﻿using System;
using System.Data;
using System.Data.Common;

namespace Songhay.Data.GenericWeb.Procedures
{
    using Songhay.Data.GenericWeb.DataTransferContracts;
    using Songhay.Data.GenericWeb.Properties;

    /// <summary>
    /// Delete procedures into the DBMS.
    /// </summary>
    public static partial class Delete
    {
        /// <summary>
        /// Deletes the specified Fragment.
        /// </summary>
        /// <param name="commonConnection">The Common connection.</param>
        /// <param name="fragment">The Fragment.</param>
        /// <returns>Returns the unique identifier of the deleted item.</returns>
        public static int? Fragment(DbConnection commonConnection, IFragment fragment)
        {
            if(commonConnection == null) throw new ArgumentNullException("commonConnection", "The Common Connection is null.");
            if(fragment == null) throw new ArgumentNullException("fragment", "The Input Fragment is null.");

            int? id = fragment.FragmentId;

            using(DbCommand cmd = commonConnection.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = Resources.DeleteFragment;

                DbParameter parameter;

                parameter = cmd.CreateParameter();
                parameter.ParameterName = "@FragmentId";
                parameter.Value = id;
                cmd.Parameters.Add(parameter);

                cmd.ExecuteNonQuery();
            }

            return id;
        }
    }
}
